<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\ArticleRequest;
use App\Model\Admin;
use App\Models\Article;
use App\Models\Cate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\ArticleCounts;

class ArticleController extends BaseController
{
    # 文章列表
    public function index(Request $request,Article $article)
    {
        # 判断是否有ajax请求
        if($request->ajax()){
            # 排序
            $order = $request->get("order")[0];
            # 排序字段的索引值
            $columns =$order["column"];
            # 排序的方法
            $dir = $order["dir"];
            # 排序的字段
            $find = $request->get("columns")[$columns]["data"];
            # 搜索
            # 日期
            $datemin = $request->get("datemin");
            $datemax = $request->get("datemax");
            # 标题
            $title = $request->get("title");

            $builer = Article::when($datemax,function ($query) use ($datemax){
                $datemax = date("Y-m-d 00:00:00",strtotime($datemax));
                $query->where("created_at",">=",$datemax);
            })->when($datemin,function ($query) use ($datemin){
                $datemin = date("Y-m-d 23:59:59",strtotime($datemin));
                $query->where("created_at","<=",$datemin);
            })->when($title,function ($query) use ($title){
                $query->where("title","like","%{$title}%");
            });

            # 记录总数
            $count = $builer->count();
            # 分页数
            $offset =$request->get("start");
            $limit =$request->get("length");
            $data = $builer->orderBy($find,$dir)->offset($offset)->limit($limit)->get();
            dump($data);
            return [
                "draw" => $request->get("draw"),
                # 记录总数
                "recordsTotal" => $count,
                "recordsFiltered" =>$count,
                "data" => $data
            ];
       }
        return view("admin.article.index",compact("article"));
    }
    
    public function count(Request $request){
        $art_id = $request->get("art_id");
        $data = ArticleCounts::select(\DB::raw("sum(click) as count,rid"))->with("renting")->where("art_id",$art_id)->groupBy("rid")->get();
        // dump($data);die;
        return view("admin.article.count",compact("data"));
    }
    public function show($id){
        dump(request()->url());
    }
    # 添加文章
    public function create()
    {
        $data = Cate::get()->toArray();
        $data = treeLevel($data);
        return view("admin.article.create",compact("data"));
    }

    # 处理图片


    # 添加数据处理
    public function store(ArticleRequest $request)
    {
        $data = $request->except(["-token","file"]);
        Article::create($data);
        return redirect( route("admin.article.index"))->with("success","添加文章成功");
//        dump($request);
    }

    # 修改数据的页面展示
    public function edit(Article $article)
    {
        $cateData = Cate::get()->toArray();
        $cateData = treeLevel($cateData);
        return view("admin.article.edit",compact("cateData","article"));
    }

    # 修改数据和处理
    public function update(ArticleRequest $request, Article $article)
    {
        # 修改数据
        $article->update($request->except(["_token","_method","file"]));
        return redirect(route("admin.article.index"))->with("success","修改成功");
    }
    # 删除图片处理
    public function deletePic(Request $request){
        $path = $request->get("path");
        $findepath = public_path($path);
        # 删除
        if(file_exists($findepath)){
            unlink($findepath);
        }
        return ["status" => 0,"msg" => "删除成功"];
    }
    # 删除数据
    public function destroy(Article $article)
    {
        $article->delete();
        return ["msg"=>"删除成功"];
    }
}
